<template>
  <div class="w">
    <div class="name" v-if="this.url == 'name'">
      <div class="header">
        <i class="iconfont icon-fanhui" @click="back"></i>
        <span class="title">修改昵称</span>
      </div>
      <div class="name_info">
        <div class="info_title">
          <span>请输入新的昵称</span>
        </div>
        <div class="info_input">
          <el-form ref="userRef" :model="userForm" :rules="userRules">
            <el-form-item prop="name">
              <el-input v-model="userForm.name" clearable placeholder="请输入昵称" class="input"></el-input>
            </el-form-item>
          </el-form>
        </div>
        <div class="info_button">
          <el-button @click="summit">确定</el-button>
        </div>
      </div>
    </div>
    <div class="phone" v-if="this.url == 'phone'">
      <div class="first" v-if="active == 1">
        <div class="header">
          <i class="iconfont icon-fanhui" @click="back"></i>
          <span class="title">修改手机号</span>
        </div>
        <div class="phone_info">
          <div class="phone_info_title">
            <span>请输入您需要绑定的新手机号</span>
          </div>
          <div class="phone_info_input">
            <el-form ref="userRef" :model="userForm" :rules="userRules">
              <el-form-item prop="phone">
                <el-input v-model="userForm.phone" clearable placeholder="请输入新手机号" class="input"></el-input>
              </el-form-item>
            </el-form>
          </div>
          <div class="phone_info_button">
            <el-button @click="sendCode">获取验证码</el-button>
          </div>
        </div>
      </div>
      <div class="second" v-if="active == 2">
        <div class="header">
          <i class="iconfont icon-fanhui" @click="back"></i>
          <span class="title">修改手机号</span>
        </div>
        <div class="phone_info">
          <div class="phone_info_title">
            <span>请输入验证码</span>
          </div>
          <div class="phone_info_input">
            <el-form ref="userRef" :model="userForm" :rules="userRules">
              <el-form-item prop="code">
                <el-input v-model="userForm.code" clearable placeholder="请输入验证码" class="input"></el-input>
              </el-form-item>
            </el-form>
          </div>
          <div class="phone_info_button">
            <el-button @click="compareCode">确定</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    // 手机号验证规则
    var checkPhone = (rule, value, cb) => {
      const regPhone = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      if (regPhone.test(value)) {
        return cb();
      }
      cb(new Error('手机号不合法'));
    };
    return {
      url: '',
      userInfo: [],
      id: '',
      userForm: {
        name: '',
        phone: '',
        code: '',
      },
      userRules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        phone: [
          { validator: checkPhone, trigger: 'blur' },
          { required: true, message: '手机号不能为空', trigger: 'blur' },
        ],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
      },
      active: 1,
    };
  },

  created() {
    this.url = this.$route.query.url;
    this.id = this.$route.query.id;
    this.getUserInfo();
  },

  methods: {
    back() {
      this.$router.push('/setting');
    },
    async getUserInfo() {
      const { data: res } = await this.$http.get('user/getById/' + this.id);
      if (res.code == 1) {
        this.userInfo = res.data;
        return;
      } else {
        return this.$message.error('信息获取失败');
      }
    },
    async summit() {
      this.$refs.userRef.validate(async (valid) => {
        if (valid) {
          this.userInfo.name = this.userForm.name;
          const { data: res } = await this.$http.put('user/updateUserPassword', this.userInfo);
          if (res.code == 1) {
            this.getUserInfo();
            window.localStorage.removeItem('userInfo');
            localStorage.setItem('userInfo', JSON.stringify(this.userInfo));
            this.$router.push('/my');
            return this.$message.success('昵称修改成功');
          } else {
            return this.$message.error('修改失败');
          }
        } else {
          return this.$message.error('表单校验失败');
        }
      });
    },
    sendCode() {
      this.$refs.userRef.validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.$http.post('user/sendCode/' + this.userForm.phone);
          if (res.code == 1) {
            this.active = this.active + 1;
            return this.$message.success(res.data);
          } else {
            return this.$message.error(res.msg);
          }
        } else {
          return this.$message.error('表单校验失败');
        }
      });
    },
    compareCode() {
      this.$refs.userRef.validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.$http.post('user/compareCode', {
            phone: this.userForm.phone,
            code: this.userForm.code,
            id: this.userInfo.id,
          });
          if (res.code == 1) {
            window.localStorage.removeItem('userInfo');
            localStorage.setItem('userInfo', JSON.stringify(res.data));
            this.$router.push('/my');
            return this.$message.success('手机号修改成功');
          } else {
            return this.$message.error(res.msg);
          }
        } else {
          return this.$message.error('表单校验失败');
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.w {
  height: 100vh;
  width: 100%;
  background-color: #f3f3f3;
  .name {
    .header {
      background-color: #fff;
      height: 50px;
      color: #0a0a0a;
      text-align: center;
      position: relative;
      margin-bottom: 20px;
      .icon-fanhui {
        position: absolute;
        font-size: 25px;
        line-height: 50px;
        left: 10px;
      }
      .title {
        font-size: 20px;
        line-height: 50px;
      }
    }
    .name_info {
      padding: 15px;
      .info_title {
        font-size: 17px;
        letter-spacing: 1px;
        margin-bottom: 10px;
      }
      .info_input {
        :deep(.el-input__inner) {
          border-radius: 12px;
          height: 50px;
        }
      }
      .info_button {
        :deep(.el-button) {
          border-radius: 10px;
          background-color: #fdc201;
          color: #31372e;
          margin-top: 30px;
          width: 100%;
          height: 50px;
          font-size: 16px;
          letter-spacing: 2px;
        }
      }
    }
  }
  .phone {
    .header {
      background-color: #fff;
      height: 50px;
      color: #0a0a0a;
      text-align: center;
      position: relative;
      margin-bottom: 20px;
      .icon-fanhui {
        position: absolute;
        font-size: 25px;
        line-height: 50px;
        left: 10px;
      }
      .title {
        font-size: 20px;
        line-height: 50px;
      }
    }
    .phone_info {
      padding: 50px;
      .phone_info_title {
        font-size: 22px;
        text-align: center;
        margin-bottom: 40px;
      }
      .phone_info_input {
        :deep(.el-input__inner) {
          border-radius: 12px;
          height: 60px;
        }
      }
      .phone_info_button {
        :deep(.el-button) {
          border-radius: 10px;
          background-color: #fdc201;
          color: #31372e;
          margin-top: 30px;
          width: 100%;
          height: 50px;
          font-size: 16px;
          letter-spacing: 1px;
        }
      }
    }
  }
}
</style>

